package ace.module.oauth2.server.core.impl.authentication.ace.factory.impl;

import ace.module.oauth2.server.api.callback.Oauth2AceAuthenticationCallbackApi;
import ace.module.oauth2.server.api.callback.Oauth2AceAuthorizationCallbackApi;
import ace.module.oauth2.server.api.enums.Oauth2RegisteredClientAuthConfigApiImplTypeEnum;
import ace.module.oauth2.server.core.impl.authentication.ace.factory.CallbackApiBeanFactory;
import ace.module.oauth2.server.core.impl.authentication.ace.factory.CallbackApiFactory;
import ace.module.oauth2.server.core.impl.authentication.ace.factory.CallbackApiFeignFactory;
import ace.module.oauth2.server.core.impl.dao.entity.Oauth2RegisteredClientAuthConfig;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 认证实现接口工厂.
 *
 * @author caspar
 */
@AllArgsConstructor
@Component
public class CallbackApiFactoryImpl implements CallbackApiFactory {


  private final CallbackApiBeanFactory callbackApiBeanFactory;
  private final CallbackApiFeignFactory callbackApiFeignFactory;

  @Override
  public Oauth2AceAuthenticationCallbackApi getOrCreateAuthCallbackApi(Oauth2RegisteredClientAuthConfig oauth2RegisteredClientAuthConfig) {
    if (Oauth2RegisteredClientAuthConfigApiImplTypeEnum.BEAN.getCode().equals(oauth2RegisteredClientAuthConfig.getApiImplType())) {
      return this.callbackApiBeanFactory.getOrCreateAuthCallbackApi(oauth2RegisteredClientAuthConfig);
    } else if (Oauth2RegisteredClientAuthConfigApiImplTypeEnum.REST.getCode().equals(oauth2RegisteredClientAuthConfig.getApiImplType())) {
      return this.callbackApiFeignFactory.getOrCreateAuthCallbackApi(oauth2RegisteredClientAuthConfig);
    } else {
      throw new RuntimeException("不支持的apiImplType." + oauth2RegisteredClientAuthConfig.getApiImplType());
    }
  }

  @Override
  public Oauth2AceAuthorizationCallbackApi getOrCreateUserInfoCallbackApi(Oauth2RegisteredClientAuthConfig oauth2RegisteredClientAuthConfig) {
    if (Oauth2RegisteredClientAuthConfigApiImplTypeEnum.BEAN.getCode().equals(oauth2RegisteredClientAuthConfig.getApiImplType())) {
      return this.callbackApiBeanFactory.getOrCreateUserInfoCallbackApi(oauth2RegisteredClientAuthConfig);
    } else if (Oauth2RegisteredClientAuthConfigApiImplTypeEnum.REST.getCode().equals(oauth2RegisteredClientAuthConfig.getApiImplType())) {
      return this.callbackApiFeignFactory.getOrCreateUserInfoCallbackApi(oauth2RegisteredClientAuthConfig);
    } else {
      throw new RuntimeException("不支持的apiImplType." + oauth2RegisteredClientAuthConfig.getApiImplType());
    }
  }
}
